package com.luyu;


import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.function.Supplier;


@Component
public class RedisLockUtil {


    private static RedissonClient client;


    @Autowired
    @Lazy
    public  void setRedisson(RedissonClient redissonClient) {
        client = redissonClient;
    }



    public static boolean lockSupplier(String key, Supplier<Boolean> supplier){
            boolean executeSuccess=false;
            RLock lock = client.getLock(key);
            lock.lock();
            try {
                if (lock.isLocked()){
                    executeSuccess=supplier.get();
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
            finally {
                lock.unlock();
                return executeSuccess;
            }
    }






}
